package com.bw.ivideoproject.home

import android.content.Intent
import android.os.Bundle
import android.widget.Toast
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import com.bw.ivideoproject.R
import com.bw.ivideoproject.databinding.FragmentHomeBinding
import com.bw.ivideoproject.entity.SimpleVideoEntity
import com.bw.lib_mvvm.BaseFragment
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayout.OnTabSelectedListener
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

class HomeFragment : BaseFragment<FragmentHomeBinding>() {

    private val viewModel: HomeViewModel by lazy {
        ViewModelProvider(this)[HomeViewModel::class.java]
    }
    private var isClear: Boolean = true
    private lateinit var simpleVideoAdapter: SimpleVideoAdapter
    private var channelId: String = ""
    private var page = 0
    private var pageSize = 10

    override fun bindLayout(): Int {
        return R.layout.fragment_home
    }

    override fun initView() {
        binding.tabType.addOnTabSelectedListener(object : OnTabSelectedListener {
            override fun onTabSelected(tab: TabLayout.Tab?) {
                isClear = true
                channelId = tab?.tag as String
                viewModel.getSimpleVideo(channelId, page, pageSize)
                binding.rvVideo.scrollToPosition(0)
            }

            override fun onTabUnselected(tab: TabLayout.Tab?) {
            }

            override fun onTabReselected(tab: TabLayout.Tab?) {
            }

        })
        simpleVideoAdapter = SimpleVideoAdapter(requireContext())
        binding.rvVideo.apply {
            this.adapter = simpleVideoAdapter
            this.layoutManager = LinearLayoutManager(requireContext())
        }
        binding.smart.setOnRefreshLoadMoreListener(object : OnRefreshLoadMoreListener {
            override fun onRefresh(refreshLayout: RefreshLayout) {
                isClear = true
                page=0
                viewModel.getSimpleVideo(channelId, page, pageSize)
            }

            override fun onLoadMore(refreshLayout: RefreshLayout) {
                isClear = false
                page++
                viewModel.getSimpleVideo(channelId, page, pageSize)
            }
        })
        simpleVideoAdapter.setOnItemClickListener(object:SimpleVideoAdapter.OnItemClickListener{
            override fun OnItemClick(item: SimpleVideoEntity) {
                val intent = Intent(requireContext(), DetailActivity::class.java).apply {
                    this.putExtra("video",item)
                }
                startActivity(intent)
            }
        })
        binding.search.setOnClickListener {
            startActivity(Intent(requireActivity(),SearchActivity::class.java))
        }
    }

    override fun initData() {
        viewModel.getSimpleVideoType()
    }

    override fun handleObserver() {
        super.handleObserver()
        viewModel.typeLiveData.observe(this) {
            for (simpleVideoTypeEntity in it) {
                binding.tabType.addTab(
                    binding.tabType.newTab().setText(simpleVideoTypeEntity.typename)
                        .setTag(simpleVideoTypeEntity.channelid)
                )
            }
        }

        viewModel.videoLiveData.observe(this) {
            binding.smart.finishLoadMore()
            binding.smart.finishRefresh()
            simpleVideoAdapter.refresh(it, isClear)
        }

        viewModel.filedLiveData.observe(this) {
            showMsg(it)
        }
    }

}